package com.project.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.config.LoginThreadLocal;
import com.project.dto.RefundOrderDto;
import com.project.dto.SupplyRefundRecordDto;
import com.project.entity.*;
import com.project.mapper.*;
import com.project.service.IUserOrderRefundService;
import com.project.util.ConvertBeanUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

/**
 * 退货管理
 */
@Service
@Transactional
public class UserOrderRefundServiceImpl implements IUserOrderRefundService {

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    private IUserOrderRefundMapper userOrderRefundMapper;
    @Autowired
    private IUserOrderRefundRecordMapper recordMapper;
    @Autowired
    private ISupplyRefundMapper refundMapper;
    @Autowired
    private IUserOrderInfoProductMapper orderInfoProductMapper;
    @Autowired
    private IUserOrderMapper orderMapper;
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private IBalanceMapper balanceMapper;
    @Autowired
    private ISupplyGoodsMapper supplyGoodsMapper;

    @Override
    public void UserRefund(long refundId, int status, String cause) {
        //查找用户退货订单对象
        UserOrderRefundEntity userOrderRefundEntity = userOrderRefundMapper.selectById(refundId);
        //查询用户订单对象
        QueryWrapper<UserOrderEntity> qw = new QueryWrapper<>();
        qw.eq("o_orderRef", userOrderRefundEntity.getOrderRef());
        UserOrderEntity userOrderEntity = orderMapper.selectOne(qw);

        //判断支付方式
        if (userOrderEntity.getModeOfPayment() == 1) {
            //如果是预存款付款
            QueryWrapper<UserEntity> qw2 = new QueryWrapper<>();
            qw2.eq("u_num", userOrderEntity.getUserRef());
            UserEntity user = userMapper.selectOne(qw2);
            user.setBalance(user.getBalance() + userOrderEntity.getTotalPrice());
            userMapper.updateById(user);
        }
        QueryWrapper<UserOrderRefundRecordEntity> qw3 = new QueryWrapper<>();
        qw3.eq("r_refundRef", userOrderRefundEntity.getRefundRef());
        qw3.eq("r_verifyStatus", 0);
        UserOrderRefundRecordEntity refundRecordEntity = recordMapper.selectOne(qw3);

        userOrderRefundEntity.setVerifyStatus(status);
        refundRecordEntity.setVerifyStatus(status);
        refundRecordEntity.setVerifyName(LoginThreadLocal.getLoginVerify().getName());
        if (status == 2) {
            //审核不通过，加入原因
            refundRecordEntity.setCause(cause);
        }
        userOrderRefundMapper.updateById(userOrderRefundEntity);
        recordMapper.updateById(refundRecordEntity);

        //--------------------------------发送到mq
            RefundOrderDto refundOrderDto = new RefundOrderDto();
            refundOrderDto.setRet_id(String.valueOf(userOrderRefundEntity.getRefundRef()));
            refundOrderDto.setStatus(status);
            refundOrderDto.setDate(LocalDate.now());
            refundOrderDto.setRefuseReason(cause);
            //Java对象转化为JSON对象
            String jsonObject = JSONObject.toJSONString(refundOrderDto);
            rabbitTemplate.convertAndSend("topicExchangeTwo", "rabbit.UserOrderRefundSupplierVerifyResult", jsonObject);

        if (status == 1) {
            refundOrderDto.setRet_id(userOrderRefundEntity.getOrderRef());
            String js = JSONObject.toJSONString(refundOrderDto);
            rabbitTemplate.convertAndSend("topicExchangeTwo", "rabbit.sendUserRefundOrder", js);
        }

    }

//    @Override
//    public void SupplyRefund(SupplyRefundEntity supplyOrderRefundRecordEntity) {
//        //将供应商订单状态修改
//        SupplyRefundEntity supplyOrderRefundRecordEntity1 = refundMapper.selectById(supplyOrderRefundRecordEntity.getRefundId());
//        supplyOrderRefundRecordEntity1.setVerifyStatus(supplyOrderRefundRecordEntity.getVerifyStatus());
//        refundMapper.updateById(supplyOrderRefundRecordEntity1);
//
//        //--------------------------------发送到mq
//        SupplyRefundRecordDto supplyRefundRecordDto =
//                ConvertBeanUtil.copy(supplyOrderRefundRecordEntity1, SupplyRefundRecordDto.class);
//        //Java对象转化为JSON对象
//        String jsonObject = JSONObject.toJSONString(supplyRefundRecordDto);
//        rabbitTemplate.convertAndSend("topicExchangeTwo", "rabbit.ConfirmTender", jsonObject);
//    }

//    @Override
//    public void refundMoney(SupplyRefundEntity supplyOrderRefundRecordEntity) {
//        SupplyRefundEntity supplyOrderRefundRecordEntity1 = refundMapper.selectById(supplyOrderRefundRecordEntity.getRefundId());
//        supplyOrderRefundRecordEntity1.setRefundStatus(supplyOrderRefundRecordEntity.getRefundStatus());
//        refundMapper.updateById(supplyOrderRefundRecordEntity1);
//        //查出本地数据库金额
//        BalanceEntity balanceEntity = balanceMapper.selectById(1);
//        balanceEntity.setMoney(balanceEntity.getMoney() + supplyOrderRefundRecordEntity.getOrderMoney());
//        balanceMapper.updateById(balanceEntity);
//
//    }

    @Override
    public IPage<UserOrderRefundEntity> findByItemUser(int pageNO, int pageSize, int status) {
        return userOrderRefundMapper.selectPage(new Page<>(pageNO, pageSize), new QueryWrapper<UserOrderRefundEntity>().eq("r_verifyStatus", status));
    }

    @Override
    public UserOrderRefundEntity findByIdUser(String refundId) {

        UserOrderRefundEntity userOrderRefundEntity = userOrderRefundMapper.selectById(refundId);


        QueryWrapper<UserOrderInfoProductEntity> qw = new QueryWrapper<>();
        qw.eq("fk_orderRef", userOrderRefundEntity.getOrderRef());
        List<UserOrderInfoProductEntity> userOrderRefundInfoProductEntityList =
                orderInfoProductMapper.selectList(qw);
        userOrderRefundEntity.setUserOrderInfoProductEntityList(userOrderRefundInfoProductEntityList);

        return userOrderRefundEntity;

    }

    @Override
    public IPage<SupplyRefundEntity> findByItemSupply(int pageNO, int pageSize) {
        return refundMapper.selectPage(new Page<>(pageNO, pageSize), null);
    }

    @Override
    public SupplyRefundEntity findByIdSupply(Integer id) {

        SupplyRefundEntity supplyRefundEntity = refundMapper.selectById(id);
        long supplyRef = supplyRefundEntity.getSupplyRef();
        QueryWrapper<SupplyGoodsEntity> qw = new QueryWrapper<>();
        qw.eq("fk_confirmRef", supplyRef);
        List<SupplyGoodsEntity> supplyGoodsEntityList = supplyGoodsMapper.selectList(qw);
        supplyRefundEntity.setSupplyGoodsEntityList(supplyGoodsEntityList);
        return supplyRefundEntity;
    }


}
